﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DeepTalkRenderServiceLib;

namespace t_DeepTalkRenderServiceLib
{
    [TestClass]
    public class t_NextUpdateTimeCalculator
    {
        [TestMethod]
        public void TestBasicTimes()
        {
            var now = DateTime.Now;
            Assert.AreEqual(13.0, NextUpdateTimeCalculator.RawTimeToUpdate(now - TimeSpan.FromDays(1), now + TimeSpan.FromDays(+1), now - TimeSpan.FromDays(21)).TotalDays, "Long before - wait until about 1 week before!");
            Assert.AreEqual(24.0, NextUpdateTimeCalculator.RawTimeToUpdate(now - TimeSpan.FromDays(1), now + TimeSpan.FromDays(+1), now - TimeSpan.FromDays(2)).TotalHours, "In progress # of hours is not working");
            Assert.AreEqual(24.0, NextUpdateTimeCalculator.RawTimeToUpdate(now - TimeSpan.FromDays(-1), now + TimeSpan.FromDays(+1), now - TimeSpan.FromDays(7)).TotalHours, "In progress # of hours is not working");
            Assert.AreEqual(4.0, NextUpdateTimeCalculator.RawTimeToUpdate(now - TimeSpan.FromDays(1), now + TimeSpan.FromDays(+1), now).TotalHours, "In progress # of hours is not working");
            Assert.AreEqual(4.0, NextUpdateTimeCalculator.RawTimeToUpdate(now, now + TimeSpan.FromDays(+1), now-TimeSpan.FromHours(1)).TotalHours, "About to be in progress should also be 4");
            Assert.AreEqual(1.0, NextUpdateTimeCalculator.RawTimeToUpdate(now, now + TimeSpan.FromDays(+1), now - TimeSpan.FromHours(5)).TotalHours, "About to be in progress should also be 4");
            Assert.AreEqual(12.0, NextUpdateTimeCalculator.RawTimeToUpdate(now, now + TimeSpan.FromDays(+1), now + TimeSpan.FromHours(25)).TotalHours, "Just Done Conference shoudl be a little longer");
            Assert.AreEqual(24.0, NextUpdateTimeCalculator.RawTimeToUpdate(now, now + TimeSpan.FromDays(+1), now + TimeSpan.FromDays(2)).TotalHours, "Just Done Conference shoudl be a little longer");
            Assert.AreEqual(1, NextUpdateTimeCalculator.RawTimeToUpdate(now, now + TimeSpan.FromDays(+1), now + TimeSpan.FromDays(7)).TotalDays, "Just Done Conference shoudl be a little longer");
            Assert.AreEqual(4, NextUpdateTimeCalculator.RawTimeToUpdate(now, now + TimeSpan.FromDays(+1), now + TimeSpan.FromDays(10)).TotalDays, "Just Done Conference shoudl be a little longer");
            Assert.AreEqual(4, NextUpdateTimeCalculator.RawTimeToUpdate(now, now + TimeSpan.FromDays(+1), now + TimeSpan.FromDays(21)).TotalDays, "Just Done Conference shoudl be a little longer");
            Assert.AreEqual(31, NextUpdateTimeCalculator.RawTimeToUpdate(now, now + TimeSpan.FromDays(+1), now + TimeSpan.FromDays(35)).TotalDays, "Just Done Conference shoudl be a little longer");
            Assert.AreEqual(31, NextUpdateTimeCalculator.RawTimeToUpdate(now, now + TimeSpan.FromDays(+1), now + TimeSpan.FromDays(6*30)).TotalDays, "Just Done Conference shoudl be a little longer");
            Assert.AreEqual(31, NextUpdateTimeCalculator.RawTimeToUpdate(now, now + TimeSpan.FromDays(+1), now + TimeSpan.FromDays(6 * 30)).TotalDays, "Just Done Conference shoudl be a little longer");
            Assert.AreEqual(0, NextUpdateTimeCalculator.RawTimeToUpdate(now, now + TimeSpan.FromDays(+1), now + TimeSpan.FromDays(7 * 30)).TotalDays, "Just Done Conference shoudl be a little longer");
        }

        [TestMethod]
        public void TestBasicTimeWithTalkUpdate()
        {
            var now = DateTime.Now;
            Assert.AreEqual(31, NextUpdateTimeCalculator.TimeToUpdate(0, now, now + TimeSpan.FromDays(+1), now + TimeSpan.FromDays(6 * 30)).TotalDays, "Just Done Conference shoudl be a little longer");
            Assert.AreEqual(1, NextUpdateTimeCalculator.TimeToUpdate(1, now, now + TimeSpan.FromDays(+1), now + TimeSpan.FromDays(6 * 30)).TotalDays, "Just Done Conference shoudl be a little longer");
        }

        [TestMethod]
        public void TestICHEPZero()
        {
            var startTime = Convert.ToDateTime("2010-07-22T08:00:00");
            var endTime = Convert.ToDateTime("2010-07-28T18:00:00");
            var now = Convert.ToDateTime("2010-07-20T10:24:00");
            Assert.AreEqual(24, NextUpdateTimeCalculator.TimeToUpdate(0, startTime, endTime, now).TotalHours, "Expecting updates of about 12 hours");
        }

        [TestMethod]
        public void TestHiggsWorkshop()
        {
            var startTime = Convert.ToDateTime("2010-07-29T08:30:00");
            var endTime = Convert.ToDateTime("2010-07-31T18:00:00");
            var now = Convert.ToDateTime("2010-07-20T10:24:00");
            Assert.AreEqual(46.1, NextUpdateTimeCalculator.TimeToUpdate(0, startTime, endTime, now).TotalHours, "Expecting updates of about 1 day");
        }
    }
}
